package xml;

import context.*;

import java.io.IOException;
import java.io.Reader;
import java.util.Hashtable;


//*****************************************************************************
//*** CLASS: XMLElementReader
//*****************************************************************************
public class XMLElementReader implements Debug.Constants
{ //private static final boolean DEBUG=true; // Overriding Debug.Constants.DEBUG
  private Reader reader;

  public interface Listener
  {
    public void treatXML(XMLElement elt);
  }
  
  // ***************************************************************************
  // *** CONSTRUCTOR: XMLElementReader
  // ***************************************************************************
  public XMLElementReader(Reader reader) { this.reader=reader; }
  
  // ***************************************************************************
  // *** METHOD: close
  // ***************************************************************************
  public void close() throws IOException
  { reader.close(); }
  
  // ***************************************************************************
  // *** METHOD: parse
  // ***************************************************************************
  public void parse(Listener listener) throws IOException
  { if(DEBUG) Debug.log("XMLElementReader'run","XMLElementReader -- Started");
    XMLElement currentElement=null;
    MiniXmlParser parser=new MiniXmlParser(reader);
    parser.setRelaxed(true); // Aksepter div. errors
    WHILE:while(true)
    { int type=parser.getNextToken();
      switch(type)
      { case MiniXmlParser.START_TAG:
          { String name=parser.getName();
//          if(LOG_XML) Debug.log("XMLElementReader.parse","<<  START: "+name+", depth="+depth);
            Hashtable attributes=parser.getAttributes();
   		    currentElement=new XMLElement(currentElement,name,attributes);
          } break;
          
        case MiniXmlParser.END_TAG:
          { String text=parser.getText();
            if(text!=null && text.length()>0)
            {
        	  //  if(LOG_XML) Debug.log("XMLElementReader.parse","<<  TEXT: "+text);
              if(currentElement!=null) currentElement.addText(text);
            }
//          if(LOG_XML) Debug.log("XMLElementReader.parse","<<  END: "+parser.getName()+", depth="+depth);
//            if(depth==0)
//            { if(LOG_XML) Debug.log("XMLElementReader.parse","<<  STREAM CLOSED: "+stream.editEndTag());
//              listener.closed(stream);
//            }
            if(currentElement!=null)
            { XMLElement parent=currentElement.getParent();
              if(parent==null)
              { if(LOG_XML) Debug.log("XMLElementReader.parse","<<  READ: "+currentElement);
                listener.treatXML(currentElement);
              }
              else parent.addChild(currentElement);
              currentElement=parent;
            }
          } break;
          
        case MiniXmlParser.END_OF_FILE:
               { if(LOG_XML) Debug.log("XMLElementReader.parse","<<  END OF FILE");
                 break WHILE;
               }
      }
    }
  }

}
